package article

import (
	"ErgoGo/internal/pkg/app"
	"ErgoGo/pkg/database"
	"ErgoGo/pkg/http/paginator"
	"time"

	"github.com/gin-gonic/gin"
)

// Paginate 分页内容
func Paginate(c *gin.Context, perPage int) (articles []Article, paging paginator.Paging) {
	paging = paginator.Paginate(
		c,
		database.DB.Model(Article{}).Order("created_at DESC"),
		&articles,
		app.V1URL(database.TableName(&Article{})),
		perPage,
	)
	return
}

func Get(id string) Article {
	var article Article
	database.DB.Where("id = ?", id).First(&article)
	return article
}

func Delete(id string) {
	database.DB.Where("id = ?", id).Delete(&Article{})
}

func BatchDelete(ids []string) error {
	return database.DB.Where("id IN ?", ids).Delete(&Article{}).Error
}

func GetTodayArticles() int {
	var count int64
	database.DB.Model(&Article{}).Where("created_at >= ?", time.Now().Format("2023-01-01")).Count(&count)
	return int(count)
}

func GetTotalArticles() int {
	var count int64
	database.DB.Model(&Article{}).Count(&count)
	return int(count)
}

func GetLastCreatedArticle() Article {
	var article Article
	database.DB.Order("created_at DESC").First(&article)
	return article
}

func GetLastPublishArticle() Article {
	var article Article
	database.DB.Where("status = ?", 1).Order("created_at DESC").First(&article)
	return article
}
